package DataStructure.SortMethod;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @time: 2021/4/26/026 22:19
 * @author: Zhurunhe
 */
public class SelectSort {
    public static void main(String[] args) {
        int max = 100000;
        int[] arr = new int[max];
        for (int i = 0; i < max; i++) {
            arr[i] = (int) (Math.random() * max);
        }
        //System.out.println(Arrays.toString(arr));
        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time1=simpleDateFormat.format(date1);
        System.out.println("排序前的时间为："+time1);
        selectSort(arr);
        //System.out.println(Arrays.toString(arr));
        Date date2 = new Date();
        String time2=simpleDateFormat.format(date2);
        System.out.println("排序后的时间为："+time2);
    }

    public static void selectSort(int[] arr) {
        int minIndex, min;
        for (int j = 0; j < arr.length-1; j++) {
        minIndex = j;//设置当前循环int[]中的最小值，每循环一次后会移动到下一位 用来记录位置并存放数组最小值的索引
        min = arr[j];//获得最小的值

        for (int i = j + 1; i < arr.length; i++) {
            if (min > arr[i]) {
                //通过循环进行判断是否为最小值
                //否 则需要用定义的变量来存储该最小值的位置和对应的值
                min = arr[i];
                minIndex = i;
            }
        }
        if (minIndex != j) {
            //判断是否本次循环中是否有最小值的变换 有则进行互换位置和值
            arr[minIndex] = arr[j];
            arr[j] = min;
        }

        //System.out.println("第"+(j+1)+"轮");
        //System.out.println(Arrays.toString(arr));

        }
        /*
        //2
        minIndex = 1;
        min = arr[1];

        for (int i = 1 + 1; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
                minIndex = i;
            }
        }
        if (minIndex != 1) {
            arr[minIndex] = arr[1];
            arr[1] = min;
        }
        System.out.println("第二轮");
        System.out.println(Arrays.toString(arr));

        //3
        minIndex = 2;
        min = arr[2];

        for (int i = 2 + 1; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
                minIndex = i;
            }
        }
        if (minIndex != 2) {
            arr[minIndex] = arr[2];
            arr[2] = min;
        }
        System.out.println("第三轮");
        System.out.println(Arrays.toString(arr));

        //4
        minIndex = 3;
        min = arr[3];

        for (int i = 3 + 1; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
                minIndex = i;
            }
        }
        if (minIndex != 3) {
            arr[minIndex] = arr[3];
            arr[3] = min;
        }
        System.out.println("第四轮");
        System.out.println(Arrays.toString(arr));*/
    }
}
